package com.winai.launcher.provider;

import android.content.ContentProvider;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.winai.launcher.base.db.DBHelper;
import com.winai.launcher.base.db.DB_Common;

import static com.winai.launcher.base.db.DB_Common.TABLE_NAME;

/**
 * 作者：康少
 * 时间：2020/4/3 0003
 * 说明：对外提供数据库表查询的结果
 */
public class DataContentProvider extends ContentProvider {
    private static final UriMatcher sURIMatcher = new UriMatcher(UriMatcher.NO_MATCH);
    private static final int QUERYSUCESS = 0;

    static{
        sURIMatcher.addURI("com.winai.launcher.provider", "query", QUERYSUCESS);
    }

    private DBHelper dbHelper;

    @Override
    public boolean onCreate() {
        DB_Common dbCommon = new DB_Common(getContext());
        dbHelper = dbCommon.getDbHelper();
        return false;
    }

    @Nullable
    @Override
    public Cursor query(@NonNull Uri uri, @Nullable String[] projection, @Nullable String selection, @Nullable String[] selectionArgs, @Nullable String sortOrder) {
        int code = sURIMatcher.match(uri);
        if (code == QUERYSUCESS) {
            SQLiteDatabase db = dbHelper.getWritableDatabase();
            Cursor query = db.query(TABLE_NAME, projection, selection, selectionArgs, null, null, sortOrder);
            getContext().getContentResolver().notifyChange(uri, null);
            return query;
        } else {
            throw new IllegalArgumentException("uri路径不匹配，请检查路径");
        }
    }

    @Nullable
    @Override
    public String getType(@NonNull Uri uri) {
        return null;
    }

    @Nullable
    @Override
    public Uri insert(@NonNull Uri uri, @Nullable ContentValues values) {
        return null;
    }

    @Override
    public int delete(@NonNull Uri uri, @Nullable String selection, @Nullable String[] selectionArgs) {
        return 0;
    }

    @Override
    public int update(@NonNull Uri uri, @Nullable ContentValues values, @Nullable String selection, @Nullable String[] selectionArgs) {
        return 0;
    }
}
